<!-- 底部聊天输入框组件 -->
<script setup lang="ts">
// 上传图片 API
import { uploadImage } from '@/services/consult';
import { Toast } from 'vant';
import type { UploaderAfterRead } from 'vant/lib/uploader/types';
import { ref } from 'vue';
// 定义 emit 事件
const emits = defineEmits<{
  (e: 'send-text', text: string): void;
  (e: 'send-image', img: { id: string; url: string }): void;
}>();

// 用户输入
const text = ref('');
// 输入事件处理
const sendText = () => {
  emits('send-text', text.value);
  // 清空输入框
  text.value = '';
};
// 上传图片
const sendImage: UploaderAfterRead = async (uploadResult) => {
  if (Array.isArray(uploadResult)) return;
  if (!uploadResult.file) return;
  const t = Toast.loading('正在上传');
  const data = await uploadImage(uploadResult.file);
  t.clear();
  emits('send-image', data);
};
defineProps<{
  disabled: boolean;
}>();
</script>

<template>
  <div class="room-action">
    <van-field
      :disabled="disabled"
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      v-model="text"
      @keyup.enter="sendText"
    ></van-field>
    <van-uploader :preview-image="false" :disabled="disabled" :after-read="sendImage">
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
